using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Quotes.ro.bvb.www;
using System.Collections;

namespace Quotes
{
    public partial class frmAlert : Form
    {
        private string[] symbols;
        InfoClass info = InfoClass.getInstance();
        #region error messages ?!
        public string FormatExceptionTitle = "Number error";
        public string OverflowExceptionTitle = "Number error";
        public string NullReferenceException = "Please select from all comboboxes!\n";
        public string NullReferenceExceptionTitle = "Combo error";
        public string LoadSymbolsExceptionTitle = "Error on retrieval";
        #endregion
        public string[] Symbols
        {
            get { return symbols; }
            set { symbols = value; }
        }

        public frmAlert()
        {
            InitializeComponent();
        }

        private void Alerts_Load(object sender, EventArgs e)
        {            
            LoadAlerts();
            LoadSymbols();
            PopulateComboBox();
        }

        private void LoadAlerts()
        {
            if (info.alerts.Count > 0)
            {
                listView1.Items.Clear();
                ListViewItem lvi;
                foreach (Alert a in info.alerts)
                {
                    lvi = new ListViewItem(new string[] { "", a.Symbol, a.Operator.ToString(), a.Value.ToString(), a.NType.ToString() });
                    if (a.Active)
                        lvi.Checked = true;
                    else
                        lvi.Checked = false;
                    listView1.Items.Add(lvi);
                }
            }
        }

        private void PopulateComboBox()
        {
            cmbSymbol.Items.Clear();
            for (int i = 0; i < Symbols.Length; i++)
            {
                cmbSymbol.Items.Add(Symbols[i]);
            }
            grpCreateAlert.Enabled = true;
            grpAlerts.Enabled = true;
        }

        private void LoadSymbols()
        {
            try
            {
                Symbols = new string[info.stocks.Count];
                IDictionaryEnumerator e = info.stocks.GetEnumerator();
                int i = 0;
                while (e.MoveNext())
                {
                    Symbols[i] = e.Key.ToString();
                    i++;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, LoadSymbolsExceptionTitle);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                switch (cmbType.SelectedItem.ToString())
                {
                    case "volume":
                    case "transactions":
                        Convert.ToInt64(txtValue.Text);
                        break;
                    case "value":
                        Convert.ToDecimal(txtValue.Text);
                        break;
                    case "%":
                        Convert.ToDouble(txtValue.Text);
                        break;
                }
                ListViewItem lvi = new ListViewItem(new string[] { "", cmbSymbol.SelectedItem.ToString(), cmbOperator.SelectedItem.ToString(), txtValue.Text, cmbType.SelectedItem.ToString() });
                listView1.Items.Add(lvi);
            }
            catch (FormatException ex) { MessageBox.Show(ex.Message, FormatExceptionTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); }
            catch (OverflowException ex) { MessageBox.Show(ex.Message, OverflowExceptionTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); }
            catch (NullReferenceException ex) { MessageBox.Show(NullReferenceException + ex.Message, NullReferenceExceptionTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); }
            finally
            {
                cmbOperator.SelectedItem = null; cmbSymbol.SelectedItem = null;
                cmbType.SelectedItem = null; txtValue.Text = "";
                cmbSymbol.Focus();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            info.alerts.Clear();
            foreach (ListViewItem lvi in listView1.Items)
            {
                bool active;
                OperatorType operatorType;
                NumberType type;
                switch (lvi.SubItems[2].Text)
                {
                    case "bigger":
                        operatorType = OperatorType.bigger;
                        break;
                    case "less":
                        operatorType = OperatorType.less;
                        break;
                    default://case "=":
                        operatorType = OperatorType.equal;
                        break;
                }
                switch (lvi.SubItems[4].Text)
                {
                    case "percent":
                        type = NumberType.percent;
                        break;
                    case "volume":
                        type = NumberType.volume;
                        break;
                    case "value":
                        type = NumberType.value;
                        break;
                    default://case "transactions":
                        type = NumberType.transaction;
                        break;
                }
                if (lvi.Checked)
                    active = true;
                else
                    active = false;
                info.alerts.Add(new Alert(lvi.SubItems[1].Text, operatorType, Convert.ToDecimal(lvi.SubItems[3].Text), type, active));
                }
            this.Close();
        }
   }
}